﻿using FS_Unitys.Basic;
using FS_Unitys.Log;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FS_Unitys.Unitys
{
    public class QuartzHelper
    {
        private static IScheduler scheduler;

        static QuartzHelper()
        {
            scheduler = StdSchedulerFactory.GetDefaultScheduler();
        }

        private static readonly string InnerReadToOutWrite = "InnerReadToOutWrite";
        private static readonly string OutReadToInnerWrite = "OutReadToInnerWrite";

        /// <summary>
        /// 启动开始
        /// </summary>
        public static void StartScheduler()
        {
            IJobDetail jobDetail_1 = new JobDetailImpl(InnerReadToOutWrite, typeof(InnerReadToOutWrite));
            IJobDetail jobDetail_2 = new JobDetailImpl(OutReadToInnerWrite, typeof(OutReadToInnerWrite));


            // 每天19:00-到第二天6:00
            //var trigger_1 = TriggerBuilder.Create().StartAt(DateBuilder.TodayAt(ConfigSoruce.startHour,ConfigSoruce.startMinute,0))
            //    .WithIdentity(new TriggerKey(InitPDF, InitPDF)).WithSimpleSchedule(a => a.WithIntervalInSeconds(ConfigSoruce.initSconds).RepeatForever())
            //    .EndAt(DateBuilder.TomorrowAt(ConfigSoruce.endHour,ConfigSoruce.endMinute,0)).Build();  // 19:00:00-to,6:00:00

            var trigger_1 = TriggerBuilder.Create()
              .WithIdentity(new TriggerKey(InnerReadToOutWrite, InnerReadToOutWrite)).WithSimpleSchedule(a => a.WithIntervalInSeconds(ConfigSource.innerSeconds).RepeatForever())
            .Build();

            //var trigger_1 = TriggerBuilder.Create().StartNow()  // 
            //    .WithIdentity(new TriggerKey(InitPDF, InitPDF))
            //    .WithSimpleSchedule(
            //    a => a.WithIntervalInSeconds(ConfigSoruce.initSconds).RepeatForever())
            // .EndAt(DateTimeOffset.Now.AddSeconds(60))
            // .Build();

            //var trigger_2 = TriggerBuilder.Create()
            // .WithIdentity(new TriggerKey(AutoPDF, AutoPDF)).WithSimpleSchedule(a => a.WithIntervalInSeconds(ConfigSoruce.autoSeconds).RepeatForever()).Build();

            // 每天19:00-到第二天6:00
            //var trigger_2 = TriggerBuilder.Create().StartAt(DateBuilder.TodayAt(ConfigSoruce.startHour, ConfigSoruce.startMinute, 0))
            // .WithIdentity(new TriggerKey(AutoPDF, AutoPDF))
            //.WithSimpleSchedule(a => a.WithIntervalInSeconds(ConfigSoruce.autoSeconds).RepeatForever())
            // .EndAt(DateBuilder.TomorrowAt(ConfigSoruce.endHour, ConfigSoruce.endMinute, 0)).Build();  // 19:00:00-to,6:00:00

            var trigger_2 = TriggerBuilder.Create()
           .WithIdentity(new TriggerKey(OutReadToInnerWrite, OutReadToInnerWrite)).WithSimpleSchedule(a => a.WithIntervalInSeconds(ConfigSource.outSeconds).RepeatForever())
            .Build();  // 19:00:00-to,6:00:00


            Dictionary<IJobDetail, Quartz.Collection.ISet<ITrigger>> dicJob = new Dictionary<IJobDetail, Quartz.Collection.ISet<ITrigger>>();
            Quartz.Collection.ISet<ITrigger> triggers_1 = new Quartz.Collection.HashSet<ITrigger>();
            triggers_1.Add(trigger_1); // 19:00 - 23:59


            Quartz.Collection.ISet<ITrigger> triggers_2 = new Quartz.Collection.HashSet<ITrigger>();
            triggers_2.Add(trigger_2); // 19:00 - 23:59

            dicJob.Add(jobDetail_1, triggers_1);
            dicJob.Add(jobDetail_2, triggers_2);

            scheduler.ScheduleJobs(dicJob, true);

            scheduler.Start();
        }


        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="strs"></param>
        public static void PauseTrigger()
        {
            // 正常时暂停
            if (GetTriggerState(InnerReadToOutWrite, InnerReadToOutWrite) == 0)
                scheduler.PauseTrigger(new TriggerKey(InnerReadToOutWrite, InnerReadToOutWrite));

            if (GetTriggerState(OutReadToInnerWrite, OutReadToInnerWrite) == 0)
                scheduler.PauseTrigger(new TriggerKey(OutReadToInnerWrite, OutReadToInnerWrite));

        }

        /// <summary>
        /// 恢复
        /// </summary>
        public static void ResumeTrigger()
        {
            // 暂停时恢复
            if (GetTriggerState(InnerReadToOutWrite, InnerReadToOutWrite) == 1)
                scheduler.ResumeTrigger(new TriggerKey(InnerReadToOutWrite, InnerReadToOutWrite));

            if (GetTriggerState(OutReadToInnerWrite, OutReadToInnerWrite) == 1)
                scheduler.ResumeTrigger(new TriggerKey(OutReadToInnerWrite, OutReadToInnerWrite));
        }

        public static int GetTriggerState(string name, string group)
        {
            return (int)scheduler.GetTriggerState(new TriggerKey(name, group));
        }
    }

    public class InnerReadToOutWrite : IJob
    {
        private WebUtils webUtils = new WebUtils();
        private LogHelper logHelper = new LogHelper(typeof(InnerReadToOutWrite));

        public void Execute(IJobExecutionContext context)
        {
            string url = ConfigSource.innerUrl;
            // logHelper.Info(url);
            string result = webUtils.DoGet(url, "utf-8");
            logHelper.Info(result);
        }
    }


    /// <summary>
    /// 初始化pdf上传的数据
    /// </summary>
    public class OutReadToInnerWrite : IJob
    {
        private WebUtils webUtils = new WebUtils();
        private LogHelper logHelper = new LogHelper(typeof(OutReadToInnerWrite));

        public void Execute(IJobExecutionContext context)
        {
            string url = ConfigSource.outUrl;
            //logHelper.Info(url);
            string result = webUtils.DoGet(url, "utf-8");
            logHelper.Info(result);
        }
    }
}
